﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using ComicIsland.Common.Serializer;

namespace ComicIsland.Common.Cache
{
    public class FileCache : BaseCache, ICache
    {
        private int maxItem = 2147483647;
        private ISerializer serializer;
        private IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        private static string CHCHE_DIR = "/cache/";
        private string instance;
        public string dir;
        public string CachePath
        {
            get
            {
                return FileCache.CHCHE_DIR + this.instance;
            }
        }
        public FileCache(string instanceName, int maxItemNum, ISerializer _serializer)
            : this(instanceName, maxItemNum)
        {
            this.serializer = _serializer;
        }
        public FileCache(string instanceName, int maxItemNum)
        {
            this.maxItem = maxItemNum;
            this.serializer = new ByteSerializer();
            this.instance = instanceName;
            this.dir = FileCache.CHCHE_DIR + this.instance;
            if (!this.storage.DirectoryExists(this.dir))
            {
                this.storage.CreateDirectory(this.dir);
                return;
            }
            string[] fileNames = this.storage.GetFileNames(this.dir + "/*");
            string[] array = fileNames;
            for (int i = 0; i < array.Length; i++)
            {
                string text = array[i];
                this.cacheKeysList.Add(text);
            }
        }
        public void add(string key, object obj)
        {
            while (this.cacheKeysList.Count >= this.maxItem)
            {
                string text = this.cacheKeysList[0];
                this.cacheKeysList.RemoveAt(0);
                string text2 = FileCache.CHCHE_DIR + this.instance + "/" + text;
                try
                {
                    if (this.storage.FileExists(text2))
                    {
                        this.storage.DeleteFile(text2);
                    }
                }
                catch
                {
                }
            }
            string md5String = MD5.GetMd5String(key);
            string text3 = this.dir + "/" + md5String;
            try
            {
                IsolatedStorageFileStream isolatedStorageFileStream = this.storage.OpenFile(text3, (FileMode)4, (FileAccess)3);
                this.serializer.Serialize(isolatedStorageFileStream, obj);
                isolatedStorageFileStream.Close();
            }
            catch
            {
            }
            this.cacheKeysList.Add(md5String);
        }
        public object get(string key)
        {
            string md5String = MD5.GetMd5String(key);
            if (this.cacheKeysList.Contains(md5String))
            {
                string text = this.dir + "/" + md5String;
                if (this.storage.FileExists(text))
                {
                    try
                    {
                        IsolatedStorageFileStream isolatedStorageFileStream = this.storage.OpenFile(text, (FileMode)4, (FileAccess)1, (FileShare)1);
                        object result = this.serializer.Deserialize(isolatedStorageFileStream);
                        isolatedStorageFileStream.Close();
                        return result;
                    }
                    catch
                    {
                    }
                }
                return null;
            }
            return null;
        }
        public bool exist(string key)
        {
            string md5String = MD5.GetMd5String(key);
            if (this.cacheKeysList.Contains(md5String))
            {
                string text = this.dir + "/" + md5String;
                if (this.storage.FileExists(text))
                {
                    return true;
                }
            }
            return false;
        }
        public void delete(string key)
        {
            string md5String = MD5.GetMd5String(key);
            if (this.cacheKeysList.Contains(md5String))
            {
                this.cacheKeysList.Remove(md5String);
                string text = FileCache.CHCHE_DIR + this.instance;
                string text2 = text + "/" + MD5.GetMd5String(key);
                try
                {
                    if (this.storage.FileExists(text2))
                    {
                        this.storage.DeleteFile(text2);
                    }
                }
                catch
                {
                }
            }
        }
        public long size()
        {
            long num = 0L;
            if (this.storage.DirectoryExists(this.dir))
            {
                string[] fileNames = this.storage.GetFileNames(this.dir + "/");
                for (int i = 0; i < fileNames.Length; i++)
                {
                    string text = fileNames[i];
                    using (IsolatedStorageFileStream isolatedStorageFileStream = this.storage.OpenFile(this.dir + "/" + text, (FileMode)3))
                    {
                        num += isolatedStorageFileStream.Length;
                    }
                }
            }
            return num;
        }
    }
}
